package com.antu.first.service.impl;

import com.antu.first.bean.Hotel;
import com.antu.first.bean.Merchant;
import com.antu.first.dao.HotelDao;
import com.antu.first.dao.MerchantDao;
import com.antu.first.dto.MerchantOperationExecution;
import com.antu.first.enums.MerchantStateEnum;
import com.antu.first.exceptions.MerchantOperationException;
import com.antu.first.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/3/7.
 */
@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantDao merchantDao;
    @Autowired
    private HotelDao hotelDao;

    @Override
    public MerchantOperationExecution addMerchant(Merchant currentMerchant, Merchant merchant) {
        try {
            //检验该管理员是否是ROOT管理员
            Merchant parent = merchantDao.queryMerchantById(currentMerchant.getmId());
            if(parent.getmType() != 0){
                return new MerchantOperationExecution(MerchantStateEnum.NOT_ROOT_MANAGER);
            }
            //检查设置的账号是否已经存在
            Merchant merchant1 = merchantDao.queryMerchantByUsername(merchant.getmUsername());
            if(merchant1 != null){
                return new MerchantOperationExecution(MerchantStateEnum.HAS_EXISTS);
            }
            //如果该用户是ROOT管理员，则进行添加操作
            int effectNum = merchantDao.insertMerchant(merchant);
            if(effectNum != 1){
                return new MerchantOperationExecution(MerchantStateEnum.ERROR_HOTEL_ID);
            }else{
                return new MerchantOperationExecution(MerchantStateEnum.SUCCESS, merchant);
            }
        } catch (Exception e) {
            throw new MerchantOperationException("在执行新增管理员操作时出现异常，异常信息："+e.getMessage());
        }
    }

    @Override
    public MerchantOperationExecution modifyMerchant(Merchant merchant) {
        try {
            merchant.setmLastEditTime(new Date());
            int effectNum = merchantDao.modifyMerchant(merchant);
            if (effectNum != 1) {
                return new MerchantOperationExecution(MerchantStateEnum.ERROR);
            }else{
                return new MerchantOperationExecution(MerchantStateEnum.SUCCESS);
            }
        } catch(Exception e) {
            throw new MerchantOperationException("在修改管理员信息的过程中出现错误，错误信息："+e.getMessage());
        }
    }

    @Override
    public MerchantOperationExecution deleteMerchant(Merchant currentMerchant, int mId) {
        try {
            //检查当前账号是否是Root账号
            Merchant merchant = merchantDao.queryMerchantById(currentMerchant.getmId());
            if(merchant==null || merchant.getmType()!=0){
                return new MerchantOperationExecution(MerchantStateEnum.CAN_NOT_DELE);
            }
            int effectNum = merchantDao.deleteMerchant(mId);
            if(effectNum != 1){
                return new MerchantOperationExecution(MerchantStateEnum.ERROR);
            }else{
                return new MerchantOperationExecution(MerchantStateEnum.SUCCESS);
            }
        } catch (Exception e) {
            throw new MerchantOperationException("在执行删除商家子用户的过程中发生错误，错误信息："+e.toString());
        }
    }

    @Override
    public MerchantOperationExecution queryAlls(int hId) {
        try {
            List<Merchant> merchants = merchantDao.queryAlls(hId);
            if(merchants.size() == 0){
                return new MerchantOperationExecution(MerchantStateEnum.EMPTY_LIST);
            }else if(merchants.size()>0){
                MerchantOperationExecution execution = new MerchantOperationExecution(MerchantStateEnum.SUCCESS, merchants);
                execution.setCount(merchants.size());
                return execution;
            }else{
                return new MerchantOperationExecution(MerchantStateEnum.ERROR);
            }
        } catch (Exception e) {
            throw new MerchantOperationException("MerchantService.queryAlls-在查询所有子账户的过程中发生错误，错误信息："+e.toString());
        }
    }

    @Override
    public Merchant queryMerchantById(int mId) {
        return merchantDao.queryMerchantById(mId);
    }
}
